:- module(match_list, [show_match/2, main/0], []).

:- use_module(library(iso_byte_char)).
:- use_module(library(format)).
:- use_module(library(read), [read/1]).
:- use_module(library(aggregates), [findall/3]).
:- use_module(search(match)).
:- use_module(search(utils)).

main:-
        ask_for_options(MatchMode, Capabilities),
        show_match(MatchMode, Capabilities).
main:- 
        exit.

ask_for_options(MatchMode, Capabilities):-
        findall(M, match_mode(M), ListMM),
        repeat,
          format("Required match mode (~w): ", [ListMM]),
          read(MatchMode),
        member(MatchMode, ListMM), !,
        format("Capabilities to match (list of atoms): ", []),
        read(Capabilities).
        

show_match(Mode, Wanted):-
        (
            atom(Wanted) ->
            WantedList = [Wanted]
        ;
            Wanted = WantedList
        ),
        match(Mode, WantedList, Flooded, Capabilities, Packages),
        nl,
        show_list('Initially required capabilities: ', WantedList),
        show_list('Packages needed: ', Packages),
        show_list('Unmatched capabilities: ', Flooded),
        show_list('Capabilities generated due to chaining: ', Capabilities),
        display('======================================================='),
        nl,
        display('More solutions? (y/n) '),
        get_char(YN),
        get_char(_NewLine),
        YN = n,
        exit.

exit:-
        format("No more solutions --- execution finished~n", []).

